/**
 * <p>Title: ServiceZMCP0A.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-24 下午03:31:31  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.cp.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlan;
import com.baosight.sczx.common.zm.cInterface.cp.ProfessionalPlan;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanZC;

/**
 * 打钻计划明细
 * <p>
 * Title: ServiceZMCP15.java
 * </p>
 */
public class ServiceZMCP15 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>
	 * Function: initLoad
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询
	 * <p>
	 * Function: query
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * <p>
	 * return value: ServiceZMCP70
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2011-7-6下午03:58:03 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {

		String fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		if (fdProfessionalplanId != null)
			StaticTools.setQueryParameters(inInfo, "fdProfessionalplanId", fdProfessionalplanId, true);// 添加查询条件

		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		if (fdProduceplanId != null)
			StaticTools.setQueryParameters(inInfo, "fdProduceplanId", fdProduceplanId, true);// 添加查询条件

		EiInfo outInfo = inInfo;
		if (fdProfessionalplanId != null || fdProduceplanId != null)
			outInfo = super.query(inInfo, "ZMCPProducePlanZC.query", new ZMCPProducePlanZC());
		else
			outInfo.addBlock("result").setBlockMeta(new ZMCPProducePlanZC().eiMetadata);

		outInfo.addBlock(Engineering.getEngineeringDetail("cmes.cp.engineering", "2", "打钻", "1", true));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,
				UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(getHoleType());
		// 设置每月可以修改所有计划的起始日期和终止日期
		outInfo.set("fdModifyStartDate", SystemSettingCP.getInstance().getModifyStartDate());// 计划修改的起始日期
		outInfo.set("fdModifyEndDate", SystemSettingCP.getInstance().getModifyEndDate());// 计划修改的终止日期
		inInfo.set("currentDate", StaticTools.getDate());// 计划修改的终止日期
		return outInfo;
	}

	/**
	 * 新增
	 * <p>
	 * Function: insert
	 * </p>
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo outinfo = new EiInfo();
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_ZC"));
			_dom.setFdProfessionalplanId(inInfo.getString("fdProfessionalplanId"));
			_dom.setFdProduceplanId(inInfo.getString("fdProduceplanId"));
			_dom.setFdPlanYear(inInfo.getString("fdPlanYear"));
			_dom.setFdPlanMonth(inInfo.getString("fdPlanMonth"));
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());// 中文名
			this.dao.insert("ZMCPProducePlanZC.insert", _dom);
			// this.dao.update("ZMCPProducePlanZC.updateProfessionalplanSum",_dom);
			// this.dao.update("ZMCPProducePlanZC.updateProduceplanSum",_dom);
			// this.dao.update("ZMCPProfessionalPlan.updateProfessionalplanSum1",_dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}

	/**
	 * 修改
	 * <p>
	 * Function: update
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * <p>
	 * return value: ServiceZMCP70
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2011-7-6下午03:58:24 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo outInfo = new EiInfo();
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());// 中文名
			this.dao.update("ZMCPProducePlanZC.update", _dom);
			// this.dao.update("ZMCPProducePlanZC.updateProfessionalplanSum",_dom);
			// this.dao.update("ZMCPProducePlanZC.updateProduceplanSum",_dom);
			// this.dao.update("ZMCPProfessionalPlan.updateProfessionalplanSum1",_dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}

	/**
	 * 删除
	 * <p>
	 * Function: delete
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * <p>
	 * return value: ServiceZMCP70
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2011-7-6下午03:58:35 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo outInfo = new EiInfo();
		ZMCPProducePlanZC _dom = new ZMCPProducePlanZC();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCPProducePlanZC.delete", _dom);
			// this.dao.update("ZMCPProducePlanZC.updateProfessionalplanSum",_dom);
			// this.dao.update("ZMCPProducePlanZC.updateProduceplanSum",_dom);
			// this.dao.update("ZMCPProfessionalPlan.updateProfessionalplanSum1",_dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}

	/**
	 * 钻孔类别下拉框
	 * <p>
	 * Function: initValidateMonthCombobox
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * <p>
	 * return value:EiBlock
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2011-8-2下午12:51:24 张志芳
	 * </p>
	 *
	 */
	private EiBlock getHoleType() {
		EiBlock block = new EiBlock("cmes.cp.holeType");

		EiColumn column = new EiColumn("value");
		block.addMeta(column);
		column = new EiColumn("display");
		block.addMeta(column);

		block.addRows(InitialData.getInstance().getEiBlockByNo("cmes.cp.holeType").getRows());
		block.addRows(InitialData.getInstance().getEiBlockByNo("cmes.cp.holeType2").getRows());
		return block;
	}

	/**
	 * 判断专项计划或生产计划是否未上报,true表示计划未上报,false表示计划已上报
	 */
	public EiInfo isNotReport(EiInfo inInfo) {
		boolean isNotReport = false;
		if (inInfo.getString("fdProfessionalplanId") != null) {// 专业计划
			isNotReport = ProfessionalPlan.validate(inInfo.getString("fdProfessionalplanId"));
			if (isNotReport == false) {
				inInfo.setStatus(2);
				inInfo.setMsg("当月的专业计划已经上报或作废，不能操作！");
			}
		} else if (inInfo.getString("fdProduceplanId") != null) {// 生产计划
			isNotReport = ProducePlan.validate(inInfo.getString("fdProduceplanId"));
			if (isNotReport == false) {
				inInfo.setStatus(2);
				inInfo.setMsg("当月的生产计划已经上报或作废，不能操作！");
			}
		}
		return inInfo;
	}
}
